<template>
  <view class="container">
    <view class="card">
      <textarea
        v-model="inputText"
        placeholder="请输入你的问题…"
        class="input-area"
        :class="{ focused: isFocused }"
        @focus="isFocused = true"
        @blur="isFocused = false"
      />
      <button
        type="primary"
        @click="callMoonshot"
        :disabled="loading"
        class="submit-btn"
      >
        <span v-if="loading" class="loading-indicator"></span>
        {{ loading ? '思考中…' : '获取 AI 回答' }}
      </button>
    </view>

    <view v-if="result" class="result-card">
      <text class="result-title">AI 回答：</text>
      <text class="result-content">{{ result }}</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      inputText: '',
      result: '',
      loading: false,
      isFocused: false,
      // 请替换为你的 Moonshot API 密钥
      apiKey: 'sk-dAW9QmHYLSCq3rdevtKJujrUtkBQFTdtgKkO7kjV5DwvK4IH'
    };
  },
  methods: {
    async callMoonshot() {
      if (!this.inputText.trim()) {
        uni.showToast({ title: '请输入内容', icon: 'none' });
        return;
      }
      this.loading = true;
      this.result = '';

      try {
        const res = await uni.request({
          url: 'https://api.moonshot.cn/v1/chat/completions',
          method: 'POST',
          header: {
            'Content-Type': 'application/json',
            Authorization: `Bearer ${this.apiKey}`
          },
          data: {
            model: 'moonshot-v1-8k', // 可选 8k/32k/128k
            messages: [
              { role: 'user', content: this.inputText }
            ],
            temperature: 0.7,
            stream: false
          }
        });

        if (res.statusCode === 200 && res.data.choices?.[0]?.message?.content) {
          this.result = res.data.choices[0].message.content;
        } else {
          throw new Error(res.data.error?.message || '接口返回异常');
        }
      } catch (err) {
        console.error('Moonshot API 调用失败:', err);
        uni.showToast({
          title: '调用失败：' + (err.message || '未知错误'),
          icon: 'none',
          duration: 3000
        });
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>

<style>
.container {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
  min-height: 100vh;
  background-color: #f5f7fa;
}

.card {
  background-color: #ffffff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  margin-bottom: 24px;
}

.input-area {
  width: 100%;
  height: 140px;
  border: 1px solid #e0e3e9;
  padding: 15px;
  margin-bottom: 16px;
  border-radius: 8px;
  font-size: 16px;
  resize: none;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.input-area:focus {
  outline: none;
}

.input-area.focused {
  border-color: #4096ff;
  box-shadow: 0 0 0 3px rgba(64, 150, 255, 0.1);
}

.submit-btn {
  width: 100%;
  padding: 14px;
  background-color: #4096ff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 0;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.submit-btn:hover:not(:disabled) {
  background-color: #3086f5;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(64, 150, 255, 0.2);
}

.submit-btn:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(64, 150, 255, 0.2);
}

.submit-btn:disabled {
  background-color: #a0cfff;
  cursor: not-allowed;
}

.result-card {
  margin-top: 24px;
  padding: 20px;
  background-color: white;
  border-radius: 12px;
  line-height: 1.7;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  animation: fadeIn 0.5s ease-out;
}

.result-title {
  font-weight: 600;
  display: block;
  margin-bottom: 12px;
  color: #1d2129;
  font-size: 18px;
  padding-bottom: 10px;
  border-bottom: 1px solid #f2f3f5;
}

.result-content {
  white-space: pre-wrap;
  color: #4e5969;
  font-size: 16px;
}

/* 加载动画 */
.loading-indicator {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 1s ease-in-out infinite;
  display: inline-block;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@keyframes fadeIn {
  from { 
    opacity: 0;
    transform: translateY(10px);
  }
  to { 
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式调整 */
@media (max-width: 640px) {
  .container {
    padding: 16px;
  }
  
  .card, .result-card {
    padding: 16px;
  }
  
  .input-area {
    height: 120px;
    font-size: 15px;
  }
  
  .submit-btn {
    padding: 12px;
    font-size: 15px;
  }
  
  .result-title {
    font-size: 17px;
  }
  
  .result-content {
    font-size: 15px;
  }
}
</style>
